﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using 纸片人.CalculateControls;
using 纸片人.win2dEffectControl;
using 纸片人.win2dEffectControl.DataClass;

namespace 纸片人.InterfaceFolder
{
    public abstract class ProviderDataControl :ExportDataControl, IProvider
    {


        protected DispatcherTimer timer = new DispatcherTimer();

        /// <summary>
        /// 输出连线列表
        /// </summary>
        public List<ConnectLineUIElement> ListProvideroneConnectElement { get; set; } = new List<ConnectLineUIElement>();

        public void AddProvideroneConnectElement(ConnectLineUIElement newConnectLine)
        {
            ListProvideroneConnectElement.Add(newConnectLine);
        }

        public void RemoveProvideroneConnectElement(ConnectLineUIElement newConnectLine)
        {
            ListProvideroneConnectElement.Remove(newConnectLine);
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        public ProviderDataControl()
        {
            this.Loaded += ExportDataControl_Loaded;
        }

      

        private void ExportDataControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetChildType<ProviderDataControl>(this, listProviderExportChild);
            timer.Interval = TimeSpan.FromMilliseconds(16);
            timer.Tick += Timer_Tick;
            timer.Start();
            OnLoaded();
        }


        protected virtual void Timer_Tick(object sender, object e)
        {

        }

        /// <summary>
        /// 控件加载完成
        /// </summary>
        protected virtual void OnLoaded()
        {

        }
        #region 属性
        /// <summary>
        /// 
        /// </summary>
      protected  List<ProviderDataControl> listProviderExportChild = new List<ProviderDataControl>();

       

        /// <summary>
        /// 是否是接收
        /// 获取或设置IsProvider的值
        /// </summary>  
        public bool IsProvider
        {
            get { return (bool)GetValue(IsProviderProperty); }
            set { SetValue(IsProviderProperty, value); }
        }

        /// <summary>
        /// 标识 IsProvider 依赖属性。
        /// </summary>
        public static readonly DependencyProperty IsProviderProperty =
            DependencyProperty.Register("IsProvider", typeof(bool), typeof(ProviderDataControl), new PropertyMetadata(true, OnIsProviderChanged));

        private static void OnIsProviderChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            ProviderDataControl target = obj as ProviderDataControl;
            bool oldValue = (bool)args.OldValue;
            bool newValue = (bool)args.NewValue;
            if (oldValue != newValue)
                target.OnIsProviderChanged(oldValue, newValue);
        }

        protected virtual void OnIsProviderChanged(bool oldValue, bool newValue)
        {
        }

        /// <summary>
        /// 与输入源的控件连接
        /// </summary>
        protected ConnectLineUIElement oneConnectElement;
#endregion


        /// <summary>
        /// 创建连接
        /// </summary>
        /// <param name="inputControl"></param>
        /// <param name="connectLineUIElement"></param>
        /// <param name="ementDrag"></param>
        public void CreateConnect( ref ConnectLineUIElement connectLineUIElement, ProviderDropControl ementDrag )
        {
            //输出的数据
                var exportData = EffectOutputControl.outputData;
            //输出的控件
            var exporter = exportData.Exporter;


            if (connectLineUIElement == null)
            {
                connectLineUIElement = new ConnectLineUIElement(exportData.ExportConnectElement, ementDrag, exporter, this);
                //创建连接
                exporter.OnConnectSourceInvoke(connectLineUIElement);
            }
            else
            {
                var newconnectElement = new ConnectLineUIElement(exportData.ExportConnectElement, ementDrag, exporter, this);
                connectLineUIElement.Exporter.RemoveProvider(connectLineUIElement);
                this.RemoveProvideroneConnectElement(connectLineUIElement);
                //更改连接
                //更改连接
                exporter.OnConnectSourceChangedInvoke(newconnectElement, connectLineUIElement);
                connectLineUIElement = newconnectElement;

            }
            exportData.ExportConnectElement.SetConnectState();
            exporter.AddExportoneConnectElement(connectLineUIElement);
            this.AddProvideroneConnectElement(connectLineUIElement);

        }


        ///// <summary>
        ///// 这是接收者的方法
        ///// </summary>
        public virtual void RemoveExport(ConnectLineUIElement connectElement)
        {
            //如果这里异常了，则说明，RemoveAllExport 有问题
            oneConnectElement = null;
            this.RemoveProvideroneConnectElement(connectElement);
        }

        public void RemoveAllExport()
        {
            foreach (var item in ListExportoneConnectElement)
            {
                item.Provider.RemoveExport(item);
                item.Provider.RemoveProvideroneConnectElement (item);
            }
        }

        public bool IsChartLoop(IExporter export)
        {
            if(export == this)
            {
                return true;
            }
            foreach (var item in ListExportoneConnectElement)
            {
                if(item.Provider == export)
                {
                    return true;
                }
               return item.Provider.IsChartLoop(export);
            }
            return false;
        }

 

        /// <summary>
        /// 获取所有输出和输入的元素连接元素
        /// </summary>
        public virtual List<ConnectLineUIElement> GetConnectLineAll()
        {
            var allListt = new List<ConnectLineUIElement>();
            foreach (var item in listProviderExportChild)
            {
                allListt.AddRange( item.GetConnectLineAll());
            }
            allListt.AddRange(ListExportoneConnectElement);
            allListt.AddRange(ListProvideroneConnectElement);
            return allListt;
        }

        /// 删除当前控件
        /// </summary>
        public override void Destroy()
        {
            RemoveAllExport();
            foreach (var item in ListProvideroneConnectElement)
            {
                item.Exporter.RemoveProvider(item);
            }
            timer.Stop();
            timer = null;
            List<ConnectLineUIElement> allConnectElement = GetConnectLineAll();
            //删除所有的输入连接
            DeleteAllConnnectInvoke(allConnectElement);
            ListExportoneConnectElement = null;
            listProviderExportChild = null;
        }

    }
}
